<template>
  <div class="markdown-preview fade-in" v-html="renderedMarkdown"></div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { marked } from 'marked'

const props = defineProps<{ markdown: string }>()

const renderedMarkdown = computed(() => marked.parse(props.markdown || ''))
</script>

<style>
.markdown-preview {
  min-height: 320px;
  background: linear-gradient(135deg, #f8fafc 0%, #f0f4ff 100%);
  border-radius: 18px;
  padding: 32px 36px 32px 36px;
  font-size: 15.5px;
  color: #23272f;
  box-shadow: 0 6px 32px 0 rgba(90, 80, 200, 0.12), 0 2px 8px 0 rgba(90,80,200,0.06);
  line-height: 1.85;
  word-break: break-word;
  font-family: 'Segoe UI', 'Helvetica Neue', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  text-align: left;
  transition: box-shadow 0.2s, background 0.2s;
  animation: fadeIn 0.7s;
}
.fade-in {
  animation: fadeIn 0.7s;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: none; }
}
.markdown-preview h1 {
  font-size: 2.2em;
  border-bottom: 2px solid #ececff;
  padding-bottom: 0.28em;
  margin: 1.3em 0 0.8em 0;
  font-weight: 900;
  color: #5a4fff;
  letter-spacing: 1.2px;
}
.markdown-preview h2 {
  font-size: 1.6em;
  border-bottom: 1.5px solid #ececff;
  padding-bottom: 0.18em;
  margin: 1.1em 0 0.7em 0;
  font-weight: 800;
  color: #6a8dff;
}
.markdown-preview h3 {
  font-size: 1.25em;
  margin: 1.1em 0 0.6em 0;
  font-weight: 700;
  color: #7c5cff;
}
.markdown-preview h4,
.markdown-preview h5,
.markdown-preview h6 {
  font-size: 1.05em;
  margin: 1em 0 0.4em 0;
  font-weight: 600;
  color: #8a8fa3;
}
.markdown-preview p {
  margin: 0.8em 0;
  font-size: 15.5px;
}
.markdown-preview ul,
.markdown-preview ol {
  margin: 0.8em 0 0.8em 2.2em;
  font-size: 15.5px;
  padding-left: 1.2em;
}
.markdown-preview ul {
  list-style: disc inside;
}
.markdown-preview ol {
  list-style: decimal inside;
}
.markdown-preview li {
  margin: 0.25em 0;
  line-height: 1.85;
}
.markdown-preview code {
  font-size: 13.5px;
  background: transparent; /* 去掉白底 */
  color: #fff; /* 字体改为白色 */
  border-radius: 7px;
  padding: 2.5px 7px;
  font-family: 'JetBrains Mono', 'Fira Mono', 'Consolas', 'Menlo', 'Monaco', 'Liberation Mono', 'Courier New', monospace;
  margin: 0 2px;
  box-shadow: 0 1px 2px 0 rgba(90, 80, 200, 0.04);
}
.markdown-preview pre {
  position: relative;
  font-size: 13.5px;
  background: linear-gradient(90deg, #23272f 60%, #1e2a38 100%);
  color: #fff; /* 统一为白色字体 */
  border-radius: 12px;
  padding: 20px 22px 20px 22px;
  overflow-x: auto;
  margin: 1.3em 0;
  box-shadow: 0 3px 12px 0 rgba(90, 80, 200, 0.13);
  font-family: 'JetBrains Mono', 'Fira Mono', 'Consolas', 'Menlo', 'Monaco', 'Liberation Mono', 'Courier New', monospace;
}
.markdown-preview pre::after {
  content: '复制';
  position: absolute;
  top: 10px;
  right: 18px;
  font-size: 12px;
  color: #b3b3b3;
  background: #fff3;
  border-radius: 4px;
  padding: 2px 8px;
  opacity: 0.7;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
  pointer-events: none;
}
.markdown-preview pre:hover::after {
  color: #6a8dff;
  background: #fff;
  opacity: 1;
}
.markdown-preview blockquote {
  font-size: 15.5px;
  color: #6a737d;
  border-left: 4px solid #a66cff;
  margin: 1.1em 0;
  padding: 0.7em 1.2em;
  background: #f8f6ff;
  border-radius: 8px;
  box-shadow: 0 1px 4px 0 rgba(90, 80, 200, 0.04);
}
.markdown-preview hr {
  border: none;
  border-top: 1.5px dashed #eaecef;
  margin: 2em 0;
}
.markdown-preview a {
  color: #6a8dff;
  text-decoration: underline;
  transition: color 0.2s, text-decoration-color 0.2s;
  text-underline-offset: 2px;
  text-decoration-thickness: 2px;
}
.markdown-preview a:hover {
  color: #a66cff;
  text-decoration-color: #a66cff;
}
.markdown-preview table {
  border-collapse: separate;
  border-spacing: 0;
  margin: 1.3em 0;
  width: 100%;
  font-size: 15.5px;
  background: #fff;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 2px 8px 0 rgba(90, 80, 200, 0.06);
  transition: box-shadow 0.2s;
}
.markdown-preview table:hover {
  box-shadow: 0 4px 16px 0 rgba(90, 80, 200, 0.13);
}
.markdown-preview th,
.markdown-preview td {
  border: 1px solid #e0e6f1;
  padding: 10px 18px;
}
.markdown-preview th {
  background: #f6f8fa;
  font-weight: 800;
  color: #5a4fff;
}
.markdown-preview img,
.markdown-preview p > img {
  display: block !important;
  margin: 1.2em auto !important;
  max-width: 250px !important;
  max-height: 220px !important;
  width: auto !important;
  height: auto !important;
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(90, 80, 200, 0.10);
  object-fit: contain;
  transition: box-shadow 0.2s, transform 0.2s;
}
.markdown-preview img:hover {
  box-shadow: 0 6px 24px 0 rgba(90, 80, 200, 0.18);
  transform: scale(1.04);
}
</style>
